<script setup lang="ts">
import { postPatientAddAPI } from '@/apis/patient'
import type { PostPatientAddParams } from '@/types/patient'
import { ref } from 'vue'

// 1. 收集表单数据
const formData = ref<PostPatientAddParams>({
  name: '',
  idCard: '330881196612238841',
  defaultFlag: 0,
  gender: 1,
})
// 2. 定义校验规则
const formRules = ref<UniHelper.UniFormsRules>({
  name: {
    rules: [
      { required: true, errorMessage: '请填写患者姓名' },
      { pattern: /^(?:[\u4e00-\u9fa5·]{2,16})$/, errorMessage: '请输入中文姓名' },
    ],
  },
  idCard: {
    rules: [
      { required: true, errorMessage: '请填写患者身份证号' },
      {
        pattern:
          /^\d{6}((((((19|20)\d{2})(0[13-9]|1[012])(0[1-9]|[12]\d|30))|(((19|20)\d{2})(0[13578]|1[02])31)|((19|20)\d{2})02(0[1-9]|1\d|2[0-8])|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))0229))\d{3})|((((\d{2})(0[13-9]|1[012])(0[1-9]|[12]\d|30))|((\d{2})(0[13578]|1[02])31)|((\d{2})02(0[1-9]|1\d|2[0-8]))|(([13579][26]|[2468][048]|0[048])0229))\d{2}))(\d|X|x)$/,
        errorMessage: '请输入正确的身份证号',
      },
    ],
  },
  gender: {
    rules: [
      { required: true, errorMessage: '请勾选患者性别' },
      {
        // 自定义校验规则
        validateFunction: (rule, value, data, callback) => {
          // 获取身份证号第17位
          const idCardGenderNumber = formData.value.idCard.charAt(16)
          // 判断身份证号第17位是否与选择的性别不一致
          if (Number(idCardGenderNumber) % 2 !== value) {
            callback('选择的性别与身份号中性别不一致')
          }
          // 验证通过返回 true
          return true
        },
      },
    ],
  },
})
// 3. 校验表单
const formRef = ref<UniHelper.UniFormsInstance>()
const onSubmit = async () => {
  // 校验表单
  await formRef.value?.validate?.()
  // 调用接口，注意：一个帐号最多添加 6 个患者
  await postPatientAddAPI(formData.value)
  // 跳转回患者列表页
  uni.navigateBack()
  // 提示用户
  uni.showToast({ title: '添加成功', icon: 'success' })
}

// 4. Switch 开关组件切换事件
const onChangeDefaultFlag = (event: UniHelper.SwitchOnChangeEvent) => {
  formData.value.defaultFlag = event.detail.value ? 1 : 0
}
</script>

<template>
  <scroll-view>
    <view class="archive-page">
      <uni-forms border label-width="220rpx" ref="formRef" :model="formData" :rules="formRules">
        <uni-forms-item label="患者姓名" name="name">
          <uni-easyinput
            v-model="formData.name"
            placeholder-style="color: #C3C3C5; font-size: 32rpx"
            :input-border="false"
            :clearable="false"
            placeholder="请填写真实姓名"
          />
        </uni-forms-item>
        <uni-forms-item label="患者身份证号" name="idCard">
          <uni-easyinput
            v-model="formData.idCard"
            placeholder-style="color: #C3C3C5; font-size: 32rpx"
            :input-border="false"
            :clearable="false"
            placeholder="请填写身份证号"
          />
        </uni-forms-item>
        <uni-forms-item label="患者性别" name="gender">
          <uni-data-checkbox
            v-model="formData.gender"
            selectedColor="#16C2A3"
            :localdata="[
              { text: '男', value: 1 },
              { text: '女', value: 0 },
            ]"
          />
        </uni-forms-item>
        <uni-forms-item label="默认就诊人" name="name">
          <view class="uni-switch">
            <switch
              :checked="formData.defaultFlag === 1"
              @change="onChangeDefaultFlag"
              color="#20c6b2"
              style="transform: scale(0.7)"
            />
          </view>
        </uni-forms-item>
        <button class="uni-button" @tap="onSubmit">保存</button>
      </uni-forms>
      {{ formData }}
    </view>
  </scroll-view>
</template>

<style lang="scss">
@import './index.scss';
</style>